<?php
G::LoadClass('plugin');

set_include_path(
    PATH_CORE . "/plugins/hr/" . PATH_SEPARATOR . get_include_path()
    );


class hrPlugin extends PMPlugin
{
  function hrPlugin($sNamespace, $sFilename = null) {
       $res = parent::PMPlugin($sNamespace, $sFilename);
       $this->sFriendlyName = 'hr Plugin';
       $this->sDescription  = 'Human Resources plugin';
       $this->sPluginFolder = 'hr';
       $this->sSetupPage    = 'hr';
       $this->iVersion      = 0.98;
       //$this->iPMVersion    = 2425;
       $this->aWorkspaces   = array('workflow', 'formsharehr', 'employee');
       //$this->aWorkspaces = array('os');
       return $res;
  }

  function setup() {
    $this->registerMenu( 'setup', 'menuhr.php');
    $this->registerMenu( 'processmaker', 'menuhrOnTransit.php');
  }

  function dropTables($aTables, $sSuffix="_TEMP", $sPrefix="_"){
	//krumo("Restoring");
	//Database Connections
	$con = Propel::getConnection("workflow");
	$stmt = $con->createStatement();

	//krumo($backupTables);

	foreach($aTables as $key => $table){
		$tableBackup=$sPrefix.$table.$sSuffix;
		//krumo($table);
		//krumo($tableBackup);
		//First Search if the $tableBackup exists
		$Sqla = sprintf("SHOW TABLES LIKE '$tableBackup'");
		//krumo($Sqla);
		$rs1 = $stmt->executeQuery($Sqla, ResultSet::FETCHMODE_ASSOC);
		//krumo($rs1->getRecordCount());
		if ($rs1->getRecordCount() != 0){ //Table $tableBackup exists, so we can Restore
			//Delete a Prev. $table if exists
			$Sqla = sprintf("DROP TABLE IF EXISTS $table;");
			//krumo($Sqla);
			$rs1 = $stmt->executeQuery($Sqla, ResultSet::FETCHMODE_ASSOC);
			//Rename $tableBackup to $table in $tableBackup :: Restore
			//$Sqla = sprintf("RENAME TABLE `$tableBackup` TO `$table`");
			//krumo($Sqla);
			//$rs1 = $stmt->executeQuery($Sqla, ResultSet::FETCHMODE_ASSOC);
		}
	}

  }


  function install() {

    $aTables=array(
                   "HR_ASSIGNMENTAREAS",
                   "HR_BENEFITS",
                   "HR_CLASSIFICATIONS",
                   "HR_CONTRACTS",
                   "HR_DEGREES_SALARY",
                   "HR_DEGREE_EARNED",
                   "HR_DEPARTMENTS",
                   "HR_DIRECT_DEPOSIT",
                   "HR_DISTRICTS",
                   "HR_DISTRICT_PROPERTY",
                   "HR_EDUCATION",
                   "HR_EEO_CATEGORY",
                   "HR_EMAILS_EMPLOYEE",
                   "HR_EMPLOYEE",
                   "HR_EMPLOYEE_ADDRESS",
                   "HR_EMPLOYEE_BENEFIT",
                   "HR_EMPLOYEE_CONTACTS",
                   "HR_EVALUATIONS",
                   "HR_GENDERS",
                   "HR_HIGH_GRADES",
                   "HR_IMMIGRATIONS_DETAILS",
                   "HR_JOBS",
                   "HR_JOBS_STATUS",
                   "HR_JOBS_TITLES",
                   "HR_LEAVE_AMOUNTS",
                   "HR_LEAVE_HISTORY",
                   "HR_LICENSES",
                   "HR_LOCATIONS",
                   "HR_LOW_GRADES",
                   "HR_MARITALS_STATUS",
                   "HR_PAY_PLAN",
                   "HR_PAY_UNIT",
                   "HR_PHONES_EMPLOYEE",
                   "HR_POSITIONS",
                   "HR_POSITION_STATUS",
                   "HR_RELATIONSHIP",
                   "HR_SALARY_SCHEDULE",
                   "HR_STEPS_SALARY",
                   "HR_TYPES_APPOINTMENT",
                   "HR_TYPES_CERTIFICATE",
                   "HR_TYPES_CONTRACT",
                   "HR_TYPES_EMPLOYEE",
                   "HR_TYPES_LEAVE",
                   "HR_TYPES_LICENSE",
                   "HR_TYPES_PAY",
                   "HR_TYPES_PHONE",
                   "HR_TYPES_POSITION",
                   "HR_TYPES_TRANSACTION",
                   "HR_TYPES_VISA",
                   "HR_UNITS_BENEFIT",
                   "HR_PREFIX",
                   "HR_SUFFIX",
                   "HR_LENGTHS_CONTRACT",
                   "HR_FUNDS_SOURCE",
                   "HR_DRUGS_TESTS",
                   "HR_TYPES_TESTS",
                   "RPT_REPORTS"
                  );

    $this->dropTables($aTables, "", "");

    $sqlFile = PATH_PLUGINS . 'hr'. PATH_SEP . '_db' . PATH_SEP . PATH_SEP . 'hr.sql';
    //$sqlContents = file_get_contents ( PATH_PLUGINS . 'knowledgeTree'. PATH_SEP . 'data' . PATH_SEP . 'mysql' . PATH_SEP . 'schema.sql');

    $handle = @fopen( $sqlFile, "r"); // Open file form read.
    $line = '';
    if ($handle) {
        while ( !feof($handle)) { // Loop til end of file.
            $buffer = fgets($handle, 4096); // Read a line.
            if ($buffer[0] != "#" && strlen( trim($buffer)) >0) { // Check for valid lines
                $line .= $buffer;
                $buffer = trim( $buffer);
                if ( $buffer [ strlen( $buffer)-1] == ';' ) {
                    echo "$line <hr>"; 
                    $con = Propel::getConnection('workflow');
                    $stmt = $con->createStatement();
                    $rs = $stmt->executeQuery($line, ResultSet::FETCHMODE_NUM);
                    $line = '';
                }
            }
        }
        fclose($handle); // Close the file.
    }

  }
}

$oPluginRegistry =& PMPluginRegistry::getSingleton();
$oPluginRegistry->registerPlugin('hr', __FILE__);
